package com.weetop.cfw.mine.adapter

import android.view.LayoutInflater
import android.view.View
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.coorchice.library.SuperTextView
import com.weetop.cfw.R
import com.weetop.cfw.base.ui.fragment.CommonBaseFragment
import com.weetop.cfw.bean.ErrorBean
import com.weetop.cfw.bean.MyLeaseSaleInformationListBean
import com.weetop.cfw.callback.RxJavaCallBack
import com.weetop.cfw.constants.UrlConstants
import com.weetop.cfw.utils.MMKVUtils
import com.weetop.cfw.utils.RetrofitUtils
import com.weetop.cfw.utils.RxJavaUtils
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import com.zhy.view.flowlayout.TagFlowLayout

class AllLeaseSaleInformationAdapter(dataList: List<MyLeaseSaleInformationListBean.DataBean>) :
    BaseMultiItemQuickAdapter<MyLeaseSaleInformationListBean.DataBean, BaseViewHolder>(dataList) {
    private lateinit var fragment: CommonBaseFragment

    fun setContext(c: CommonBaseFragment) {
        fragment = c
    }

    init {
        addItemType(PUBLISH, R.layout.layout_item_publish_lease_sale_information)
        addItemType(UNAUDITED, R.layout.layout_item_unaudited_lease_sale_information)
    }

    companion object {
        const val PUBLISH = 0//待审核的
        const val UNAUDITED = 1//已发布的
    }


    override fun convert(helper: BaseViewHolder, item: MyLeaseSaleInformationListBean.DataBean?) {
        val textWorkshopImg = helper.getView<SuperTextView>(R.id.textWorkshopImg)
        val flowWorkshopDesc: TagFlowLayout = helper.getView(R.id.flowWorkshopDesc)

        helper.addOnClickListener(R.id.llDelete, R.id.llMotify,R.id.llTop,R.id.llRefresh)


        val tagList = ArrayList<String?>()
        if (item?.isalone?.trim() == "1") {
            tagList.add("独院")
        } else {

        }
        if (item?.haslift?.trim() == "1") {
            tagList.add("电梯")
        } else {

        }
        if (item?.floor?.trim() != "") {
            tagList.add("${item?.floor}层")
        } else {

        }
        flowWorkshopDesc.adapter = object : TagAdapter<String>(tagList) {
            override fun getView(parent: FlowLayout?, position: Int, t: String?): View {
                val textItemWorkshopDescFlowLayoutView = LayoutInflater.from(mContext)
                    .inflate(R.layout.layout_item_workshop_desc_flow, parent, false)
                val textItemWorkshopDesc =
                    textItemWorkshopDescFlowLayoutView.findViewById<SuperTextView>(R.id.textItemWorkshopDesc)
                textItemWorkshopDesc.text = tagList[position]
                return textItemWorkshopDescFlowLayoutView
            }
        }
        textWorkshopImg.setUrlImage("${UrlConstants.baseUrl}${item?.imgurl}")
        helper.setText(R.id.textWorkshopName, item?.title)
            .setText(R.id.textWorkshopAddress, "地址：${item?.areaname} ${item?.address}")
            .setText(R.id.textWorkshopArea, "面积：${item?.totalarea}㎡")

    }

    private fun deleteInfo(infoid: Int) {
        RxJavaUtils.useInSupportFragmentOnDestoryView(
            RetrofitUtils.getApi().deleteInfo(
                MMKVUtils.getToken(),
                infoid
            ), fragment, object :
                RxJavaCallBack<ErrorBean>() {
                override fun onSuccess(t: ErrorBean?) {
                    notifyDataSetChanged()
                }

                override fun onErrorResponse(throwable: Throwable?) {
                    super.onErrorResponse(throwable)
                }

                override fun onRequestError(t: ErrorBean?) {
                    super.onRequestError(t)
                }

            })
    }
}